package dbb

import (
	"strings"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/text/gstr"
	octicons "github.com/lusingander/fyne-octicons"
	"github.com/y2h/fyne_poem/menu"
	"github.com/y2h/fyne_poem/widgets"
)

func newcon() widget.ToolbarItem {
	return widget.NewToolbarAction(octicons.DatabaseIcon(), func() {
		w := widgets.EmptyWin("New database")
		msg := widget.NewLabel("")
		seldb := widget.NewRadioGroup([]string{"mysql", "pgsql"}, nil)
		seldb.SetSelected("mysql")
		seldb.Horizontal = true
		txtName := widget.NewEntry()
		txtName.SetText("root")
		txtPass := widget.NewEntry()
		txtPass.SetText("123456")
		txtHost := widget.NewEntry()
		txtHost.SetText("127.0.0.1")
		txtPort := widget.NewEntry()
		txtPort.SetText("3306")
		txtLink := widget.NewEntry()
		dbss := []string{}
		txtDbname := widget.NewEntry()
		seldbs := widget.NewSelect(dbss, func(s string) {
			txtDbname.SetText(s)
			Me.Db.GetConfig().Name = s
		})
		frm := widget.NewForm(widget.NewFormItem("Link", txtLink),
			widget.NewFormItem("Name", txtName),
			widget.NewFormItem("Password", txtPass),
			widget.NewFormItem("Host", txtHost),
			widget.NewFormItem("Port", txtPort),
		)
		str1 := gstr.Join([]string{
			gstr.Trim(txtName.Text), gstr.Trim(txtPass.Text)}, ":")
		str2 := "@tcp(" + gstr.Trim(txtHost.Text) + ":" +
			gstr.Trim(txtPort.Text) + ")/" +
			gstr.Trim(txtDbname.Text)
		txtLink.SetText(str1 + str2)
		frm.OnSubmit = func() {
			dbss = []string{}
			// "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
			str1 := gstr.Join([]string{
				gstr.Trim(txtName.Text), gstr.Trim(txtPass.Text)}, ":")
			str2 := "@tcp(" + gstr.Trim(txtHost.Text) + ":" +
				gstr.Trim(txtPort.Text) + ")/" +
				gstr.Trim(txtDbname.Text)
			txtLink.SetText(str1 + str2)
			dbs.Set("newmysql", gdb.ConfigNode{
				Type: seldb.Selected,
				Link: txtLink.Text,
			})
			Me.Db, Me.Err = gdb.New(dbs.Get("newmysql").(gdb.ConfigNode))
			if Me.Err != nil {
				flash(msg, "没有有效的数据库连接")
				menu.Glog.Error(Me.Ctx, Me.Err)
				return
			}
			sqldb := "show databases;"
			rs, err := Me.Db.Raw(sqldb).Array()
			menu.Glog.Error(Me.Ctx, err)

			for _, v := range rs {
				dbss = append(dbss, v.String())
			}
			seldbs.Options = dbss
			msg.SetText("成功连接到数据库")
		}
		frm.SubmitText = "测试连接"
		btnCon := widget.NewButton("Connect", func() {
			if txtDbname.Text == "" {
				flash(msg, "没有有效的数据库连接")
				return
			}

			str1 := gstr.Join([]string{
				gstr.Trim(txtName.Text), gstr.Trim(txtPass.Text)}, ":")
			str2 := "@tcp(" + gstr.Trim(txtHost.Text) + ":" +
				gstr.Trim(txtPort.Text) + ")/" +
				gstr.Trim(txtDbname.Text)
			txtLink.SetText(str1 + str2)
			Me.Db.GetConfig().Link = str1 + str2
			Me.Db = dbs.GetOrSetFuncLock(txtDbname.Text, func() interface{} {
				db0, _ := gdb.New(gdb.ConfigNode{
					Type: seldb.Selected,
					Name: txtDbname.Text,
					Link: txtLink.Text,
				})
				return db0

			}).(gdb.DB)
			connect1(txtDbname.Text)
		})

		btnCreateDB := widget.NewButton("Create DB", func() {
			if Me.Db == nil {
				flash(msg, "没有有效的数据库连接")
				return
			}
			sqlCreateDB := "CREATE DATABASE  IF NOT EXISTS `" +
				strings.TrimSpace(txtDbname.Text) +
				"` DEFAULT CHARACTER SET utf8mb4 /*!40100 COLLATE 'utf8mb4_general_ci' */ ;"
			_, err := Me.Db.Exec(Ctx, sqlCreateDB)
			if err != nil {
				menu.Glog.Error(Ctx, err)
				return
			}
			frm.OnSubmit()
			msg.SetText(txtDbname.Text + " database created.")
		})

		cc := container.NewVBox(seldb, frm, seldbs, txtDbname,
			container.NewGridWithColumns(2, btnCreateDB, btnCon),
		)
		w.SetContent(container.NewBorder(nil, msg, nil, nil, cc))
		w.Show()
	})
}
